package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblCTDTMonHoc;
import com.lb.jwitter.service.TblCTDTMonHocService;
import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKHocVien;
import com.lb.jwitter.service.TblDKMHDotDKHocVienService;
import com.lb.jwitter.service.TblDKMHKyHieu;
import com.lb.jwitter.service.TblDKMHKyHieuService;
import com.lb.jwitter.service.TblMenu;
import com.lb.jwitter.service.TblMenuService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblQuyenMenu;
import com.lb.jwitter.service.TblQuyenMenuService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblTaiKhoanService;
import com.lb.jwitter.service.TblThongTinHocVien;
import com.lb.jwitter.service.TblThongTinHocVienService;
import com.lb.jwitter.service.TblThongTinMonHoc;
import com.lb.jwitter.service.TblThongTinMonHocService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.ExcelToJSon;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class TblDKMHDotDKHocVienSaveFromXLSAction {


	private TblTaiKhoan taiKhoanDangNhap; 
	  
	  public TblTaiKhoan getTaiKhoanDangNhap() { 
	      return taiKhoanDangNhap; 
	  } 
	  public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
	      this.taiKhoanDangNhap = taiKhoanDangNhap; 
	  } 
	  
	  private int dotDKId;
	  
	public int getDotDKId() {
		return dotDKId;
	}
	public void setDotDKId(int dotDKId) {
		this.dotDKId = dotDKId;
	}

	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	private Message msg;
	
	  public Message getMsg() {
		return msg;
	}

	public void setMsg(Message msg) {
		this.msg = msg;
	}

	private TblDKMHDotDKHocVienService tblDKMHDotDKHocVienService; 
	
	private JSONArray jsons;

	public JSONArray getJsons() {
		return jsons;
	}
	public void setJsons(JSONArray jsons) {
		this.jsons = jsons;
	}
	
	private String destFile;
	public String getDestFile() {
		return destFile;
	}
	public void setDestFile(String destFile) {
		this.destFile = destFile;
	}
	
	private String result;
	
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public String execute() throws Exception
	{
		Date startDate = new Date();

	     try{ 	
	    	 
	         Map session = ActionContext.getContext().getSession();
	         taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	         if( taiKhoanDangNhap == null) return "noprivilege";
	         
	        ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
	        
	        TblThongTinHocVienService tblThongTinHocVienService = (TblThongTinHocVienService)  context.getBean("tbl_ThongTin_HocVienClient"); 
	        
	        tblDKMHDotDKHocVienService = (TblDKMHDotDKHocVienService) context.getBean("tbl_DKMH_DotDK_HocVienClient"); 
	        
	        jsons = ExcelToJSon.execute(destFile);
	        
	        result = tblDKMHDotDKHocVienService.createListtblDKMHDotDKHocVienByJson(dotDKId, jsons.toString(), taiKhoanDangNhap);
	        
//    		TblDKMHDotDK tblDKMHDotDK = new TblDKMHDotDK();
//    		tblDKMHDotDK.setId(new Long(dotDKId));
	        
//	        List<TblDKMHDotDKHocVien> tblDKMHDotDKHocViens = new ArrayList<TblDKMHDotDKHocVien>();
//	        
//	        List<TblThongTinHocVien> hocVien = null;
//	        for(int i = 0; i < jsons.size() ; i++){
//	        	try{
//	        		
//		    		JSONObject obj = jsons.getJSONObject(i);
////		    		result += i +":" + obj.toString()+"<br/>";
//		    		
//		    		TblDKMHDotDKHocVien tblDKMHDotDKHocVien = new TblDKMHDotDKHocVien();
//	
//		    		 //get mon hoc theo ma mon hoc
//		            List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
//		            {
//		            	 ConditionReport con = new ConditionReport(); 
//		                 con.setCol("MSSV");
//		                 con.setValue1(obj.getString("MSSV"));
//		                 con.setOperator("eq");
//		                 cons.add(con);	
//		            }
//		            hocVien = tblThongTinHocVienService.findtblThongTinHocVienByCondition(cons, 1, 1);
//	
//		            //set mon hoc
//		            if(hocVien != null && hocVien.size()>0){
//		            	tblDKMHDotDKHocVien.setHocVienId(hocVien.get(0));
//		    
//		            	Boolean khongDK = Boolean.parseBoolean(obj.getString("KhongDangKyYN").trim());
//		            	Integer soTinChiDK = Integer.parseInt(obj.getString("SoTinChiDKToiDa").trim());
//		            	//set dotDK
//
//			    		tblDKMHDotDKHocVien.setDotDKId(tblDKMHDotDK);
//			    		
//			           //set Can Bo
//			           tblDKMHDotDKHocVien.setCanBoId(taiKhoanDangNhap.getCanBoId());
//			           
//			           tblDKMHDotDKHocVien.setKhongDangKyLyDo(obj.getString("KhongDangKyLyDo"));
//			           tblDKMHDotDKHocVien.setKhongDangKyYN(khongDK);
//			           tblDKMHDotDKHocVien.setSoTCDangKyToiDa(soTinChiDK);
//			           //set ngay cap nhat
//			           DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
//			           Date dd = new Date(); 
//			           GregorianCalendar c = new GregorianCalendar(); 
//			           c.setTime(dd);
//			           XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
//			            tblDKMHDotDKHocVien.setNgayCapNhat(d); 
//			    		
//			            tblDKMHDotDKHocViens.add(tblDKMHDotDKHocVien);
////			            tblDKMHDotDKHocVienService.createtblDKMHDotDKHocVien(tblDKMHDotDKHocVien);
//		            }
//		            
//		            
//		            
//		            if((i >0 && (i+1)%1000 ==0) || (i == jsons.size()-1)){
//		            	tblDKMHDotDKHocVienService.createListtblDKMHDotDKHocVien(tblDKMHDotDKHocViens);
//		            	tblDKMHDotDKHocViens.clear();
//		            	obj = null;
//		            	tblDKMHDotDKHocVien = null;
//		            	hocVien.clear();
//		            	
//	        			
//	        		}
//	        	}catch(Exception e){
//	        		e.printStackTrace();
//	        	}
//	
//	    	}//end for json
	        
	        //start insert
//	        Date startinsertdate = new Date();
//	        tblDKMHDotDKHocVienService.createListtblDKMHDotDKHocVien(tblDKMHDotDKHocViens);
	        
	        Date enDate = new Date();
	        
	        
	        result += "Start = " + startDate.toString() + "<br/>";
//	        result += "Insert = " + startinsertdate.toString() + "<br/>";
	        result += "End = " + enDate.toString() + "<br/>";
	            return Action.SUCCESS; 
	     } 
	     catch(Exception e){ 
	             System.out.println("Loi add ky hieu from excell Action"); 
	             e.printStackTrace(); 
	             return Action.ERROR; 
	     } 

	}
	
	
	
	
	
}
